<template>
	<div class="register-page">
		<header-nav :show-btn="true" />

		<div class="register-container">
			<div class="register-card">
				<h1 class="register-title">注册</h1>

				<div class="register-options">
					<div class="register-option" :class="{ active: activeOption === 'organization' }" @click="activeOption = 'organization'">
						<div class="option-icon">
							<svg
								xmlns="http://www.w3.org/2000/svg"
								width="24"
								height="24"
								viewBox="0 0 24 24"
								fill="none"
								stroke="currentColor"
								stroke-width="2"
								stroke-linecap="round"
								stroke-linejoin="round"
							>
								<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
								<circle cx="9" cy="7" r="4"></circle>
								<path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
								<path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
							</svg>
						</div>
						<h3 class="option-title">组织注册</h3>
						<p class="option-subtitle">安装供应商</p>
					</div>

					<div class="register-option" :class="{ active: activeOption === 'owner' }" @click="activeOption = 'owner'">
						<div class="option-icon">
							<svg
								xmlns="http://www.w3.org/2000/svg"
								width="24"
								height="24"
								viewBox="0 0 24 24"
								fill="none"
								stroke="currentColor"
								stroke-width="2"
								stroke-linecap="round"
								stroke-linejoin="round"
							>
								<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
								<circle cx="12" cy="7" r="4"></circle>
							</svg>
						</div>
						<h3 class="option-title">个人注册</h3>
						<p class="option-subtitle">个人注册</p>
					</div>
				</div>

				<div class="register-form" v-if="activeOption">
					<el-form ref="registerForm" :model="form" :rules="rules" label-width="100px">
						<el-form-item label="注册方式" prop="registerMethod">
							<el-select v-model="form.registerMethod" placeholder="请选择注册方式">
								<el-option label="手机注册" value="phone"></el-option>
								<el-option label="邮箱注册" value="email"></el-option>
							</el-select>
						</el-form-item>

						<el-form-item label="手机号" prop="phone" v-if="form.registerMethod === 'phone'">
							<el-input v-model="form.phone" placeholder="请输入手机号">
								<template #prepend>
									<el-select v-model="form.areaCode" style="width: 80px">
										<el-option label="+86" value="86"></el-option>
									</el-select>
								</template>
							</el-input>
						</el-form-item>

						<el-form-item label="邮箱" prop="email" v-else>
							<el-input v-model="form.email" placeholder="请输入邮箱地址"></el-input>
						</el-form-item>

						<el-form-item label="验证码" prop="verificationCode">
							<el-input v-model="form.verificationCode" placeholder="请输入验证码">
								<template #append>
									<el-button type="warning" :disabled="countdown > 0" @click="getVerificationCode">
										{{ countdown > 0 ? `${countdown}s后重新获取` : '获取验证码' }}
									</el-button>
								</template>
							</el-input>
						</el-form-item>

						<el-form-item label="密码" prop="password">
							<el-input v-model="form.password" type="password" placeholder="6-20位字母数字组合" show-password></el-input>
						</el-form-item>

						<el-form-item label="确认密码" prop="confirmPassword">
							<el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" show-password></el-input>
						</el-form-item>

						<el-form-item>
							<el-button type="danger" class="submit-btn" @click="submitForm" :loading="loading">立即注册</el-button>
						</el-form-item>
					</el-form>
				</div>
			</div>
		</div>
	</div>
</template>

<script setup>
import './style.scss';
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import HeaderNav from '../../components/headerNav/index.vue';

const activeOption = ref('organization');
const registerForm = ref(null);
const loading = ref(false);
const countdown = ref(0);

const form = ref({
	registerMethod: 'phone',
	phone: '',
	areaCode: '86',
	email: '',
	verificationCode: '',
	password: '',
	confirmPassword: '',
});

const validatePassword = (rule, value, callback) => {
	if (!value) {
		callback(new Error('请输入密码'));
	} else if (!/^[A-Za-z0-9]{6,20}$/.test(value)) {
		callback(new Error('密码必须是6-20位的数字、字母'));
	} else {
		callback();
	}
};

const validateConfirmPassword = (rule, value, callback) => {
	if (!value) {
		callback(new Error('请再次输入密码'));
	} else if (value !== form.value.password) {
		callback(new Error('两次输入的密码不一致'));
	} else {
		callback();
	}
};

const rules = {
	registerMethod: [{ required: true, message: '请选择注册方式', trigger: 'change' }],
	phone: [
		{ required: true, message: '请输入手机号', trigger: 'blur' },
		{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
	],
	email: [
		{ required: true, message: '请输入邮箱地址', trigger: 'blur' },
		{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
	],
	verificationCode: [
		{ required: true, message: '请输入验证码', trigger: 'blur' },
		{ len: 6, message: '验证码为6位数字', trigger: 'blur' },
	],
	password: [{ validator: validatePassword, trigger: 'blur' }],
	confirmPassword: [{ validator: validateConfirmPassword, trigger: 'blur' }],
};

const getVerificationCode = () => {
	if (form.value.registerMethod === 'phone' && !form.value.phone) {
		ElMessage.warning('请输入手机号');
		return;
	}

	if (form.value.registerMethod === 'email' && !form.value.email) {
		ElMessage.warning('请输入邮箱地址');
		return;
	}

	countdown.value = 60;
	const timer = setInterval(() => {
		countdown.value--;
		if (countdown.value <= 0) {
			clearInterval(timer);
		}
	}, 1000);

	ElMessage.success('验证码已发送');
};

const submitForm = async () => {
	try {
		loading.value = true;
		await registerForm.value.validate();
		ElMessage.success('注册成功');
	} catch (error) {
		console.error('表单验证失败:', error);
	} finally {
		loading.value = false;
	}
};
</script>

<style scoped>
.register-page {
	display: flex;
	flex-direction: column;
	min-height: 100vh;
	background-color: #f5f7fa;
}

.register-container {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 2rem;
	flex-grow: 1;
}

.register-card {
	width: 100%;
	max-width: 50rem;
	padding: 2rem;
	background-color: #fff;
	border-radius: 8px;
	box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.register-title {
	text-align: center;
	margin-bottom: 2rem;
	color: #303133;
	font-size: 1.8rem;
	font-weight: 500;
}

.register-options {
	display: flex;
	justify-content: center;
	gap: 2rem;
	margin-bottom: 2rem;
}

.register-option {
	width: 12rem;
	padding: 1.5rem 1rem;
	border: 1px solid #dcdfe6;
	border-radius: 8px;
	text-align: center;
	cursor: pointer;
	transition: all 0.3s ease;
}

.register-option:hover {
	border-color: #c0c4cc;
	transform: translateY(-2px);
}

.register-option.active {
	border-color: #409eff;
	background-color: #f0f7ff;
	box-shadow: 0 4px 12px rgba(64, 158, 255, 0.1);
}

.option-icon {
	margin-bottom: 1rem;
	color: #409eff;
}

.option-title {
	margin-bottom: 0.5rem;
	font-size: 1.1rem;
	color: #303133;
	font-weight: 500;
}

.option-subtitle {
	color: #909399;
	font-size: 0.9rem;
}

.submit-btn {
	width: 50%;
	margin-top: 1rem;
}

:deep(.el-form-item__content) {
	display: flex !important;
	align-items: center !important;
	justify-content: center !important;
}

:deep(.el-form-item__label) {
	font-weight: 500;
}

:deep(.el-input__wrapper) {
	box-shadow: 0 0 0 1px var(--el-input-border-color, #dcdfe6) !important;
}

:deep(.el-input.is-focus .el-input__wrapper) {
	box-shadow: 0 0 0 1px var(--el-input-focus-border-color, #dcdfe6) !important;
}

:deep(.el-select .el-input__wrapper) {
	box-shadow: 0 0 0 1px var(--el-input-border-color, #dcdfe6) !important;
}

:deep(.el-select.is-focus .el-input__wrapper) {
	box-shadow: 0 0 0 1px var(--el-input-focus-border-color, #dcdfe6) !important;
}
</style>
